# copied from:  Final_Code_Replication_Final/02_Analysis/01_Appendix/Appendix_I/Table_I2.R

# Code for Table I2: First Stage Continued

rm(list = ls())

## ---------------------------------------
## Load Packages 
## ---------------------------------------
require('AER')
require('ivpack')
require('data.table')

## ---------------------------------------
## Load Data and Functions
## ---------------------------------------
load("../data0812.RData")

directory <- "../functions/"
functions <- list.files(directory)  
loadfunctions <- sapply(functions, FUN = function(x)source(paste0(directory, x)))

## ---------------------------------------
# Construct Instrument
## ---------------------------------------
data0812 <- constructIV(data0812)

## ---------------------------------------
#  Select Last Case Before Election
## ---------------------------------------
data0812 <- lastCase(data0812)

## ---------------------------------------
## Two Stage Least Squares Specifications
## ---------------------------------------

time.controls <- "as.factor(court_time1) + as.factor(court_time2) + as.factor(court_dow) + as.factor(court_shift) + as.factor(totOGS2)"
case.controls <-   "as.factor(any_drug_2) +  as.factor(any_violent_2) + as.factor(fire_arms_2) +  as.factor(any_rob_2) + as.factor(any_dui_2) + as.factor(prior_offender_2)"
case.controls_drug <- "as.factor(any_violent_2) + as.factor(fire_arms_2) +  as.factor(any_rob_2) + as.factor(any_dui_2)  + as.factor(prior_offender_2)"
case.controls_vio <- "as.factor(any_drug_2) + as.factor(fire_arms_2) +  as.factor(any_rob_2) + as.factor(any_dui_2)  + as.factor(prior_offender_2)"
case.controls_fire <-   "as.factor(any_drug_2) +  as.factor(any_violent_2)  +  as.factor(any_rob_2) + as.factor(any_dui_2) + as.factor(prior_offender_2)"
case.controls_dui <-   "as.factor(any_drug_2) +  as.factor(any_violent_2) + as.factor(fire_arms_2) +  as.factor(any_rob_2)  + as.factor(prior_offender_2)"
case.controls_rob <-   "as.factor(any_drug_2) +  as.factor(any_violent_2) + as.factor(fire_arms_2) + as.factor(any_dui_2) + as.factor(prior_offender_2)"

demo.controls <- "age_2012 + I(age_2012^2) + Female + as.factor(race) + vote2008 + as.factor(noteli08) + regis_before"
demo.controls_gender <- "age_2012 + I(age_2012^2) + as.factor(race) + vote2008 + as.factor(noteli08) + regis_before"

outc.1 <- "vote2012"
endo.1 <- "pti"
inst.1 <- "judgeiv"

form.1 <- formula(paste(outc.1, "~", endo.1, "+" , time.controls, "|", inst.1, "+", time.controls))
form.2 <- formula(paste(outc.1, "~", endo.1, "+" , time.controls, "+", demo.controls, "|", inst.1, "+", time.controls, "+", demo.controls))

form.2_gender <- formula(paste(outc.1, "~", endo.1, "+" , time.controls, "+", demo.controls_gender, "|", inst.1, "+", time.controls, "+", demo.controls_gender))
form.3_gender <- formula(paste(outc.1, "~", endo.1, "+" , time.controls, "+", demo.controls_gender, "+", case.controls, "|", inst.1, "+", time.controls, "+", demo.controls_gender, "+", case.controls))

form.3_drug <- formula(paste(outc.1, "~", endo.1, "+" , time.controls, "+", demo.controls, "+", case.controls_drug, "|", inst.1, "+", time.controls, "+", demo.controls, "+", case.controls_drug))
form.3_vio <- formula(paste(outc.1, "~", endo.1, "+" , time.controls, "+", demo.controls, "+", case.controls_vio, "|", inst.1, "+", time.controls, "+", demo.controls, "+", case.controls_vio))
form.3_fire <- formula(paste(outc.1, "~", endo.1, "+" , time.controls, "+", demo.controls, "+", case.controls_fire, "|", inst.1, "+", time.controls, "+", demo.controls, "+", case.controls_fire))
form.3_dui <- formula(paste(outc.1, "~", endo.1, "+" , time.controls, "+", demo.controls, "+", case.controls_dui, "|", inst.1, "+", time.controls, "+", demo.controls, "+", case.controls_dui))
form.3_rob <- formula(paste(outc.1, "~", endo.1, "+" , time.controls, "+", demo.controls, "+", case.controls_rob, "|", inst.1, "+", time.controls, "+", demo.controls, "+", case.controls_rob))

form.1.fs <- formula(paste(endo.1, "~", inst.1, "+", time.controls))
form.2.fs <- formula(paste(endo.1, "~", inst.1, "+", time.controls, "+", demo.controls))

form.2.fs_gender <- formula(paste(endo.1, "~", inst.1, "+", time.controls, "+", demo.controls_gender))
form.3.fs_gender <- formula(paste(endo.1, "~", inst.1, "+", time.controls, "+", demo.controls_gender, "+", case.controls))

form.3.fs_drug <- formula(paste(endo.1, "~", inst.1, "+", time.controls, "+", demo.controls, "+", case.controls_drug))
form.3.fs_vio <- formula(paste(endo.1, "~", inst.1, "+", time.controls, "+", demo.controls, "+", case.controls_vio))
form.3.fs_fire <- formula(paste(endo.1, "~", inst.1, "+", time.controls, "+", demo.controls, "+", case.controls_fire))
form.3.fs_dui <- formula(paste(endo.1, "~", inst.1, "+", time.controls, "+", demo.controls, "+", case.controls_dui))
form.3.fs_rob <- formula(paste(endo.1, "~", inst.1, "+", time.controls, "+", demo.controls, "+", case.controls_rob))

## ---------------------------------------
# Row 1: Gender
## ---------------------------------------
m1a1 <- ivreg(form.1, data = data0812[Male ==1])
m1a2 <- ivreg(form.2_gender, data = data0812[Male ==1])
m1a3 <- ivreg(form.3_gender, data = data0812[Male ==1])

m1d1 <- lm(form.1.fs, data = data0812[Male ==1])
m1d2 <- lm(form.2.fs_gender, data = data0812[Male ==1])
m1d3 <- lm(form.3.fs_gender, data = data0812[Male ==1])

m1a1D <- summary(m1a1, vcov = sandwich, diagnostic = T)$diagnostics[1, 3]
m1a2D <- summary(m1a2, vcov = sandwich, diagnostic = T)$diagnostics[1, 3]
m1a3D <- summary(m1a3, vcov = sandwich, diagnostic = T)$diagnostics[1, 3]

male <- data.table(round(rbind(c(robust.se(m1d1)[2, c(1)], robust.se(m1d2)[2, c(1)], robust.se(m1d3)[2, c(1)]),
                                 c(robust.se(m1d1)[2, c(2)], robust.se(m1d2)[2, c(2)], robust.se(m1d3)[2, c(2)]),
                                 c(m1a1D, m1a2D, m1a3D),
                                 c(m1a1$n, m1a2$n, m1a3$n),
                                 rep(NA, 3)),3))

m1a1 <- ivreg(form.1, data = data0812[Male ==0])
m1a2 <- ivreg(form.2_gender, data = data0812[Male ==0])
m1a3 <- ivreg(form.3_gender, data = data0812[Male ==0])

m1d1 <- lm(form.1.fs, data = data0812[Male ==0])
m1d2 <- lm(form.2.fs_gender, data = data0812[Male ==0])
m1d3 <- lm(form.3.fs_gender, data = data0812[Male ==0])

m1a1D <- summary(m1a1, vcov = sandwich, diagnostic = T)$diagnostics[1, 3]
m1a2D <- summary(m1a2, vcov = sandwich, diagnostic = T)$diagnostics[1, 3]
m1a3D <- summary(m1a3, vcov = sandwich, diagnostic = T)$diagnostics[1, 3]


female <- data.table(round(rbind(c(robust.se(m1d1)[2, c(1)], robust.se(m1d2)[2, c(1)], robust.se(m1d3)[2, c(1)]),
                               c(robust.se(m1d1)[2, c(2)], robust.se(m1d2)[2, c(2)], robust.se(m1d3)[2, c(2)]),
                               c(m1a1D, m1a2D, m1a3D),
                               c(m1a1$n, m1a2$n, m1a3$n),
                               rep(NA, 3)
                               ),3))
                               

## ---------------------------------------
# Row 2: Offense Types
## ---------------------------------------
# Drug
m1a1 <- ivreg(form.1, data = data0812[any_drug_2 ==1])
m1a2 <- ivreg(form.2, data = data0812[any_drug_2 ==1])
m1a3 <- ivreg(form.3_drug, data = data0812[any_drug_2 ==1])

m1d1 <- lm(form.1.fs, data = data0812[any_drug_2 ==1])
m1d2 <- lm(form.2.fs, data = data0812[any_drug_2 ==1])
m1d3 <- lm(form.3.fs_drug, data = data0812[any_drug_2 ==1])

m1a1D <- summary(m1a1, vcov = sandwich, diagnostic = T)$diagnostics[1, 3]
m1a2D <- summary(m1a2, vcov = sandwich, diagnostic = T)$diagnostics[1, 3]
m1a3D <- summary(m1a3, vcov = sandwich, diagnostic = T)$diagnostics[1, 3]

drug <- data.table(round(rbind(c(robust.se(m1d1)[2, c(1)], robust.se(m1d2)[2, c(1)], robust.se(m1d3)[2, c(1)]),
                       c(robust.se(m1d1)[2, c(2)], robust.se(m1d2)[2, c(2)], robust.se(m1d3)[2, c(2)]),
                       c(m1a1D, m1a2D, m1a3D),
                       c(m1a1$n, m1a2$n, m1a3$n),
                       rep(NA, 3)
),3))

# Violent
m1a1 <- ivreg(form.1, data = data0812[any_violent_2 ==1])
m1a2 <- ivreg(form.2, data = data0812[any_violent_2 ==1])
m1a3 <- ivreg(form.3_vio, data = data0812[any_violent_2 ==1])

m1d1 <- lm(form.1.fs, data = data0812[any_violent_2 ==1])
m1d2 <- lm(form.2.fs, data = data0812[any_violent_2 ==1])
m1d3 <- lm(form.3.fs_vio, data = data0812[any_violent_2 ==1])

m1a1D <- summary(m1a1, vcov = sandwich, diagnostic = T)$diagnostics[1, 3]
m1a2D <- summary(m1a2, vcov = sandwich, diagnostic = T)$diagnostics[1, 3]
m1a3D <- summary(m1a3, vcov = sandwich, diagnostic = T)$diagnostics[1, 3]


violent <- data.table(round(rbind(c(robust.se(m1d1)[2, c(1)], robust.se(m1d2)[2, c(1)], robust.se(m1d3)[2, c(1)]),
                       c(robust.se(m1d1)[2, c(2)], robust.se(m1d2)[2, c(2)], robust.se(m1d3)[2, c(2)]),
                       c(m1a1D, m1a2D, m1a3D),
                       c(m1a1$n, m1a2$n, m1a3$n),
                       rep(NA, 3)
),3))


# Firearm 
m1a1 <- ivreg(form.1, data = data0812[fire_arms_2 ==1])
m1a2 <- ivreg(form.2, data = data0812[fire_arms_2 ==1])
m1a3 <- ivreg(form.3_fire, data = data0812[fire_arms_2 ==1])

m1d1 <- lm(form.1.fs, data = data0812[fire_arms_2 ==1])
m1d2 <- lm(form.2.fs, data = data0812[fire_arms_2 ==1])
m1d3 <- lm(form.3.fs_fire, data = data0812[fire_arms_2 ==1])

m1a1D <- summary(m1a1, vcov = sandwich, diagnostic = T)$diagnostics[1, 3]
m1a2D <- summary(m1a2, vcov = sandwich, diagnostic = T)$diagnostics[1, 3]
m1a3D <- summary(m1a3, vcov = sandwich, diagnostic = T)$diagnostics[1, 3]

firearm <- data.table(round(rbind(c(robust.se(m1d1)[2, c(1)], robust.se(m1d2)[2, c(1)], robust.se(m1d3)[2, c(1)]),
                       c(robust.se(m1d1)[2, c(2)], robust.se(m1d2)[2, c(2)], robust.se(m1d3)[2, c(2)]),
                       c(m1a1D, m1a2D, m1a3D),
                       c(m1a1$n, m1a2$n, m1a3$n),
                       rep(NA, 3)
),3))

# DUI 
m1a1 <- ivreg(form.1, data = data0812[any_dui_2 ==1])
m1a2 <- ivreg(form.2, data = data0812[any_dui_2 ==1])
m1a3 <- ivreg(form.3_dui, data = data0812[any_dui_2 ==1])

m1d1 <- lm(form.1.fs, data = data0812[any_dui_2 ==1])
m1d2 <- lm(form.2.fs, data = data0812[any_dui_2 ==1])
m1d3 <- lm(form.3.fs_dui, data = data0812[any_dui_2 ==1])

m1a1D <- summary(m1a1, vcov = sandwich, diagnostic = T)$diagnostics[1, 3]
m1a2D <- summary(m1a2, vcov = sandwich, diagnostic = T)$diagnostics[1, 3]
m1a3D <- summary(m1a3, vcov = sandwich, diagnostic = T)$diagnostics[1, 3]

dui <- data.table(round(rbind(c(robust.se(m1d1)[2, c(1)], robust.se(m1d2)[2, c(1)], robust.se(m1d3)[2, c(1)]),
                       c(robust.se(m1d1)[2, c(2)], robust.se(m1d2)[2, c(2)], robust.se(m1d3)[2, c(2)]),
                       c(m1a1D, m1a2D, m1a3D),
                       c(m1a1$n, m1a2$n, m1a3$n),
                       rep(NA, 3)
),3))

# Property 
m1a1 <- ivreg(form.1, data = data0812[any_rob_2 ==1])
m1a2 <- ivreg(form.2, data = data0812[any_rob_2 ==1])
m1a3 <- ivreg(form.3_rob, data = data0812[any_rob_2 ==1])

m1d1 <- lm(form.1.fs, data = data0812[any_rob_2 ==1])
m1d2 <- lm(form.2.fs, data = data0812[any_rob_2 ==1])
m1d3 <- lm(form.3.fs_rob, data = data0812[any_rob_2 ==1])

m1a1D <- summary(m1a1, vcov = sandwich, diagnostic = T)$diagnostics[1, 3]
m1a2D <- summary(m1a2, vcov = sandwich, diagnostic = T)$diagnostics[1, 3]
m1a3D <- summary(m1a3, vcov = sandwich, diagnostic = T)$diagnostics[1, 3]

property <- data.table(round(rbind(c(robust.se(m1d1)[2, c(1)], robust.se(m1d2)[2, c(1)], robust.se(m1d3)[2, c(1)]),
                                   c(robust.se(m1d1)[2, c(2)], robust.se(m1d2)[2, c(2)], robust.se(m1d3)[2, c(2)]),
                                   c(m1a1D, m1a2D, m1a3D),
                                   c(m1a1$n, m1a2$n, m1a3$n),
                                   rep(NA, 3)
),3))

cont <- rbind(male, female, drug, violent, firearm, dui, property)
# add column labels
colnames(cont) <- c("(1)", "(2)", "(3)")
# add row labels
cont$names <-  c("Male",
                 "Std. Error",
                 "F-test", 
                 "Obs.",
                 "",
                 "Female",
                 "Std. Error",
                 "F-test", 
                 "Obs.",
                 "",
                 "Drug",
                 "Std. Error",
                 "F-test", 
                 "Obs.",
                 "",
                 "Violent",
                 "Std. Error",
                 "F-test", 
                 "Obs.",
                 "",
                 "Firearm",
                 "Std. Error",
                 "F-test", 
                 "Obs.",
                 "",
                 "DUI",
                 "Std. Error",
                 "F-test", 
                 "Obs.",
                 "",
                 "Property",
                 "Std. Error",
                 "F-test", 
                 "Obs.",
                 "")
cont <- cont[, c("names", "(1)", "(2)", "(3)")]

cat("\nPrinting Table I2: First Stage...\n")
print(cont)

